﻿using System.Text;
using System.Text.RegularExpressions;

namespace Koala.Pro.Common
{
    public class ValidateUtil
    {
        public static readonly string EmailRegex = "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$";

        public static readonly string CHZNRegex = "[\u4e00-\u9fa5]";

        public static readonly string UserNameRegex = "^([\\u4e00-\\u9fa5A-Za-z_0-9]{0,})$";

        public static readonly string PasswordCharNumberRegex = "^[A-Za-z_0-9]{6,16}$";

        public static readonly string PasswordRegex = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\\s).{6,16}$";

        public static readonly string ValidIntRegex = "^[1-9]\\d*\\.?[0]*$";

        public static readonly string NumericRegex = "^[-]?\\d+[.]?\\d*$";

        public static readonly string NumberRegex = "^[0-9]+$";

        public static readonly string IntCheck = "^[0-9]+[0-9]*$";

        public static readonly string NumberSignRegex = "^[+-]?[0-9]+$";

        public static readonly string DecimalRegex = "^[0-9]+[.]?[0-9]+$";

        public static readonly string DecimalSignRegex = "^[+-]?[0-9]+[.]?[0-9]+$";

        public static readonly string PhoneRegex = "^(\\(\\d{3,4}\\)|\\d{3,4}-)?\\d{7,8}$";

        public static readonly string MobileRegex = "^(13|15|18)\\d{9}$";

        public static readonly string PhoneMobileRegex = "^(\\(\\d{3,4}\\)|\\d{3,4}-)?\\d{7,8}$|^(13|15|18)\\d{9}$";

        public static readonly string ID15Regex = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";

        public static readonly string ID18Regex = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[A-Z])$";

        public static readonly string UrlRegex = "\\b(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]*[-A-Za-z0-9+&@#/%=~_|]";

        public static readonly string IPRegex = "^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$";

        public static readonly string Base64Regex = "[A-Za-z0-9\\+\\/\\=]";

        public static readonly string LetterRegex = "^[A-Za-z]+$";

        public static readonly string GuidRegex = "[A-F0-9]{8}(-[A-F0-9]{4}){3}-[A-F0-9]{12}|[A-F0-9]{32}";

        public static readonly string FilePathRegex = "^(?<fpath>([a-zA-Z]:\\\\)([\\s\\.\\-\\w]+\\\\)*)(?<fname>[\\w]+)(?<namext>(\\.[\\w]+)*)(?<suffix>\\.[\\w]+)";

        public static readonly string HexStringRegex = "\\A\\b[0-9a-fA-F]+\\b\\Z";

        public static readonly string CarLicenseCheck = "^([\\u4e00-\\u9fa5]|[A-Z]){1,2}[A-Za-z0-9]{1,2}-[0-9A-Za-z]{5}$";

        public static readonly string MacAddr7Check = "^([0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F])$";

        public static readonly string MacAddr6Check = "^([0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F]-[0-9A-F][0-9A-F])$";

        public static bool IsMatch(string input, string pattern)
        {
            return IsMatch(input, pattern, RegexOptions.IgnoreCase);
        }

        public static bool IsMatch(string input, string pattern, RegexOptions options)
        {
            return Regex.IsMatch(input, pattern, options);
        }

        public static int GetStringLength(string stringValue)
        {
            return Encoding.Default.GetBytes(stringValue).Length;
        }

        public static bool IsValidUserName(string userName)
        {
            int stringLength = GetStringLength(userName);
            if (stringLength >= 4 && stringLength <= 20 && Regex.IsMatch(userName, UserNameRegex))
            {
                return true;
            }
            return false;
        }

        public static bool IsValidPassword(string password)
        {
            return Regex.IsMatch(password, PasswordRegex);
        }

        public static bool IsInt(string val)
        {
            return Regex.IsMatch(val, ValidIntRegex);
        }

        public static bool IsNumeric(string inputData)
        {
            return new Regex(NumericRegex).Match(inputData).Success;
        }

        public static bool IsNumber(string inputData)
        {
            return new Regex(NumberRegex).Match(inputData).Success;
        }

        public static bool IsNumberSign(string inputData)
        {
            return new Regex(NumberSignRegex).Match(inputData).Success;
        }

        public static bool IsDecimal(string inputData)
        {
            return new Regex(DecimalRegex).Match(inputData).Success;
        }

        public static bool IsDecimalSign(string inputData)
        {
            return new Regex(DecimalSignRegex).Match(inputData).Success;
        }

        public static bool IsChinese(string inputData)
        {
            return new Regex(CHZNRegex).Match(inputData).Success;
        }

        public static int GetChineseLength(string inputData)
        {
            byte[] bytes = new ASCIIEncoding().GetBytes(inputData);
            int num = 0;
            for (int i = 0; i <= bytes.Length - 1; i++)
            {
                if (bytes[i] == 63)
                {
                    num++;
                }
                num++;
            }
            return num;
        }

        public bool IsLetter(string inputData)
        {
            return Regex.IsMatch(inputData, LetterRegex);
        }

        public static bool IsIdCard(string idCard)
        {
            if (string.IsNullOrEmpty(idCard))
            {
                return false;
            }
            if (idCard.Length == 15)
            {
                return Regex.IsMatch(idCard, ID15Regex);
            }
            if (idCard.Length == 18)
            {
                return Regex.IsMatch(idCard, ID18Regex, RegexOptions.IgnoreCase);
            }
            return false;
        }

        public static bool IsEmail(string inputData)
        {
            return new Regex(EmailRegex).Match(inputData).Success;
        }

        public static bool IsPostCode(string zip)
        {
            return new Regex("^\\d{6}$", RegexOptions.None).Match(zip).Success;
        }

        public static bool IsPhone(string phone)
        {
            return new Regex(PhoneRegex, RegexOptions.None).Match(phone).Success;
        }

        public static bool IsMobile(string mobile)
        {
            return new Regex(MobileRegex, RegexOptions.None).Match(mobile).Success;
        }

        public static bool IsPhoneAndMobile(string number)
        {
            return new Regex(PhoneMobileRegex, RegexOptions.None).Match(number).Success;
        }

        public static bool IsURL(string url)
        {
            return Regex.IsMatch(url, UrlRegex);
        }

        public static bool IsIP(string ip)
        {
            return Regex.IsMatch(ip, IPRegex);
        }

        public static bool IsDomain(string host)
        {
            Regex regex = new Regex("^\\d+$");
            if (host.IndexOf(".") == -1)
            {
                return false;
            }
            return !regex.IsMatch(host.Replace(".", string.Empty));
        }

        public static bool IsBase64String(string str)
        {
            return Regex.IsMatch(str, Base64Regex);
        }

        public static bool IsGuid(string guid)
        {
            if (string.IsNullOrEmpty(guid))
            {
                return false;
            }
            return Regex.IsMatch(guid, GuidRegex, RegexOptions.IgnoreCase);
        }

        public static bool IsFilePath(string filePath)
        {
            return Regex.IsMatch(filePath, FilePathRegex);
        }

        public static bool IsHexString(string hexString)
        {
            return Regex.IsMatch(hexString, HexStringRegex);
        }

        public static bool IsPort(string port)
        {
            bool result = false;
            int num = 0;
            int num2 = 65535;
            int result2 = -1;
            if (int.TryParse(port, out result2))
            {
                result = result2 >= num && result2 <= num2;
            }
            return result;
        }

        public static bool IsDate(string strValue)
        {
            return Regex.IsMatch(strValue, "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))");
        }

        public static bool IsDateHourMinute(string strValue)
        {
            return Regex.IsMatch(strValue, "^(19[0-9]{2}|[2-9][0-9]{3})-((0(1|3|5|7|8)|10|12)-(0[1-9]|1[0-9]|2[0-9]|3[0-1])|(0(4|6|9)|11)-(0[1-9]|1[0-9]|2[0-9]|30)|(02)-(0[1-9]|1[0-9]|2[0-9]))\\x20(0[0-9]|1[0-9]|2[0-3])(:[0-5][0-9]){1}$");
        }

        public static string CheckMathLength(string inputData, int maxLength)
        {
            if (inputData != null && inputData != string.Empty)
            {
                inputData = inputData.Trim();
                if (inputData.Length > maxLength)
                {
                    inputData = inputData.Substring(0, maxLength);
                }
            }
            return inputData;
        }

        public static string Encode(string str)
        {
            str = str.Replace("&", "&amp;");
            str = str.Replace("'", "''");
            str = str.Replace("\"", "&quot;");
            str = str.Replace(" ", "&nbsp;");
            str = str.Replace("<", "&lt;");
            str = str.Replace(">", "&gt;");
            str = str.Replace("\n", "<br>");
            return str;
        }

        public static string Decode(string str)
        {
            str = str.Replace("<br>", "\n");
            str = str.Replace("&gt;", ">");
            str = str.Replace("&lt;", "<");
            str = str.Replace("&nbsp;", " ");
            str = str.Replace("&quot;", "\"");
            return str;
        }
    }
}
